package com.kgo.eidtor.controller;

import com.kgo.eidtor.entity.LoginEntity;
import com.kgo.eidtor.entity.ModuleBean;
import com.kgo.eidtor.entity.UserBean;
import com.kgo.eidtor.entity.params.JWTParams;
import com.kgo.eidtor.service.LoginService;
import com.kgo.eidtor.service.UserService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author keepgoon
 * @Description: TODO
 * @date 2019/2/1410:49
 */
@RestController
@CrossOrigin
public class LoginController {
    @Resource
    private UserService userService;
    @Resource
    private LoginService loginService;
    @RequestMapping(value = "/login/old" ,method = RequestMethod.POST,produces="text/plain;charset=UTF-8")
    public String login(UserBean user){
        return userService.login(user);
    }


    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
    public String login(HttpServletRequest request , HttpServletResponse response, LoginEntity login){
        String tokenStr =  loginService.login(login);

        Cookie cookie = new Cookie(JWTParams.Token,tokenStr);
        response.addCookie(cookie);
        return tokenStr;
    }
}
